package com.leetCode.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * 你拥有的石头中有多少是宝石
 */
public class GemAndStone {
    //此方法太过浪费时间与空间
    public int numJewelsInStones(String J, String S) {
        //先将字符串J转换为字符类型
        char[] gems = J.toCharArray();
        char[] stones = S.toCharArray();
        int num = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (char stone : stones) {
            if (map.containsKey(stone)) {
                map.put(stone, map.get(stone) + 1);
            } else {
                map.put(stone, 1);
            }
        }
        for (char gem : gems) {
            if (map.containsKey(gem)) {
                num = num + map.get(gem);
            }
        }
        //将拥有的石头转为map：key为字符，value为拥有的数量

        return num;
    }
}
